﻿namespace GreenIsomer.Data.Entities.Ddl {
	using System;
	using System.Collections.Generic;
	using System.Collections.ObjectModel;

	public class TableCollection : Collection<Table> {

		public TableCollection() { }

		public void MergeExtendedDefinition(ExtendedDefinition extendedDefinition) {
			if(extendedDefinition == null) throw new ArgumentNullException("extendedDefinition");

			foreach(Table table in this) {

				ExtendedTableDef extendedTableDef = extendedDefinition.Tables.Find(table.Name);
				if(extendedTableDef == null) continue;

				table.SuppressDdlOutput = extendedTableDef.SuppressDdlOutput;
				table.Indexes.AddRange(extendedTableDef.Indexes);
				table.Columns.MergeExtendedDefinition(extendedTableDef);
			}

		}

		public void ReplaceDataTypes(IEnumerable<TypeConversion> typeConversions) {
			if(typeConversions == null) throw new ArgumentNullException("typeConversions");

			foreach(Table table in this) table.Columns.ReplaceDataTypes(typeConversions);
		}

		public virtual Table Find(string name) {
			foreach(Table item in this)
				if(item.Name == name) return item;
			return null;
		}
	}

}